/*
 * Created on 2004-8-12
 *
 * Ԥ�������������DMͨѶ�ͻ����¼�����
 */
package core.upintf;

import io.ReconnableAsyncSocketEventListener;
import io.stripframe.FrameProcessor;
import io.stripframe.FrameStripper;
import io.stripframe.HeaderTailStripper;
import io.stripframe.RawlStripper;

import java.nio.channels.SocketChannel;

import org.apache.log4j.Logger;

import com.seari.ssmq.client.IEventDisposal;

import config.appconfig;

import core.IEC104Server;
import core.ProcessThread;

import protocol.FrameGenerator;
import protocol.pv.PVCommand;
import protocol.pv.PVCommandException;

public class DMClientEventListener implements ReconnableAsyncSocketEventListener,IEventDisposal {
	private static Logger logger = Logger.getLogger(DMClientEventListener.class);
	private FrameStripper stripper;
	private static char frameHeader = (char)2;
	private static char frameTail = (char)3;
	private DMClient getDMClient(){
		return (DMClient)this.client;
	}
	
	private class DMClientFrameProcessor implements FrameProcessor {
		public void process(String s) {
			try {
				PVCommand command = PVCommand.genPVCommand(s);
				if (command.getCommandID().equals(PVCommand.SOCKET_BEAT)) {
					//command.decode(null);
					getDMClient().getClientBeatTask().setBeatResponseResult(true);
				}
				else {
					System.out.println(s);
					logger.info(s);
					//消息量大的消息单独开线程，其他消息并入一个线程default
					String msgid= command.getCommandID();
					if (msgid!=null){
						ProcessThread thd=IEC104Server.processThreads.get(msgid);
						if (thd !=null){
							thd.feedCommand(command);
						}else{
							thd= IEC104Server.processThreads.get("others");
							if (thd !=null){
								thd.feedCommand(command);
							}	
						}
					}
					
				}
			} catch (PVCommandException e) {
				logger.error("pv command error", e);						
			} catch (Throwable e) {
				logger.error("runtime error", e);
			}
		}
	}
	private UpInterface client;
	
	public DMClientEventListener(UpInterface client) {
		this.client = client;
	}
	
	public void onConnect(SocketChannel channel) {
		logger.info(channel.socket().getRemoteSocketAddress()+" connect DM client successfully");			
		//DM2 NOT NEED STRIPPER
		PVCommand gen = new PVCommand("i_am");
		gen.put("type", appconfig.getStrProp("msg.worktype"));
	    gen.put("user", "a0001");
		client.write(gen);
	    if ( client instanceof DM2Client){
	    	stripper = new RawlStripper(); //use raw stripper
	    } else {
			stripper = new HeaderTailStripper(frameHeader, frameTail);
	    }
		stripper.attachObserver(new DMClientFrameProcessor());

	}
	
	public void onRead(SocketChannel channel, String msg) {
		logger.info("read from "+channel.socket().getRemoteSocketAddress()+" -- "+msg);
		if (stripper != null)
			stripper.putString(msg);	 //onread use to put string
	}
	
	public void onClose(SocketChannel channel) {
		logger.info("socket disconnected from "+channel.socket().getRemoteSocketAddress());		
	}

	public void onDisconnect(SocketChannel channel) {
		this.onClose(channel);
	}

	public void onReceived(SocketChannel channel, String text) {
		this.onRead(channel, text);
	}

	public void onRegistered(int clientid, boolean isRegistered) {
		logger.info(" ClientId:" + clientid + " Registered:" + isRegistered);
	}
	public static void main(String[] args) {
		PVCommand gen = new PVCommand("i_am");
		gen.put("type", "shdk");
		gen.put("user", "a0001");
		//System.out.println(gen.output());
		String s= FrameGenerator.genFrame(gen.output());
		System.out.println(s);
		
		PVCommand gen1 = new PVCommand("now_time");
		gen1.put("msg", "13234455;1345");
		String s1= FrameGenerator.genFrame(gen1.output());
		System.out.println(s1);
		
		PVCommand gen3 = new PVCommand("dc_data");
		gen3.put("msg", "13234455;1345;111");
		//gen3.put("procid", "a0001");
		String s3= FrameGenerator.genFrame(gen3.output());
		System.out.println(s3);
		
		
		
		
	}
}
